package com.zjh.es;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zjh.es.entry.Products;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.io.IOException;
import java.util.Date;

@SpringBootTest
public class RestHighLevelClientDocumentQuery {
    @Autowired
    private RestHighLevelClient client;

    @Test
    void testTermQuery() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.termQuery("description", "真好吃"))
//                .must(QueryBuilders.termQuery("price", 8.9));
        BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery().must(QueryBuilders.matchQuery("description", "真好吃"));
        sourceBuilder.query(boolQueryBuilder);
        sourceBuilder.from(0).size(10).sort("price", SortOrder.DESC);

        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        for (SearchHit hit : search.getHits().getHits()) {
            System.out.println(hit.getSourceAsString());
        }



    }

    @Test
    void insertDoc() throws IOException {
        Products products = new Products();
        products.setId(8);
        products.setTitle("日本豆腐");
        products.setPrice(7.8);
        products.setCreateAt(new Date());
        products.setDescription("日本豆腐，真好吃");
        IndexRequest indexRequest = new IndexRequest("products");
        indexRequest.source(new ObjectMapper().writeValueAsString( products), XContentType.JSON);
        client.index(indexRequest, RequestOptions.DEFAULT);
    }

    @Test
    void testQuery() throws IOException {
        SearchRequest searchRequest = new SearchRequest("products");

        SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
        sourceBuilder.query(QueryBuilders.matchAllQuery());

        searchRequest.source(sourceBuilder);
        SearchResponse search = client.search(searchRequest, RequestOptions.DEFAULT);
        search.getHits().forEach(hit -> {
            String sourceAsString = hit.getSourceAsString();
            Products products = null;
            try {
                products = new ObjectMapper().readValue(sourceAsString, Products.class);
            } catch (JsonProcessingException e) {
                throw new RuntimeException(e);
            }
            System.out.println(products);
        });
    }
}
